package pdm.traj.dummyfrontend;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import pdm.traj.conf.TrajConf;
import pdm.traj.entity.TrackPoint2;
import pdm.traj.service.TrajDataService;
import pdm.traj.service.UpdateService;

public class FileDataImporter {
//	private TrajDataService trajDataService;
	private UpdateService updateService;
	private double entryPerSec = 1000.0;
	private long groupLength = 10;
//	private long cycle = (long) (1000.0 * groupLength / entryPerSec);
	private long cycle = 5000;
	
	public void init() {
//		try {
//			entryPerSec = Double.parseDouble(TrajConf.getInstance().getProperty(TrajConf.DUMMY_IMPORT_ENTRY_PER_SEC));
//		} catch (NumberFormatException nfe) {
//			nfe.printStackTrace();
//		}
//		cycle = (long) (1000.0 * groupLength / entryPerSec);
//		if (cycle == 0) {
//			cycle = 1;
//		}
	}
	
	/**
	 * 慢慢地从文件导入数据到redis
	 * @param filePath - 数据源路径
	 * @param blockSize - 每一次导入条目数
	 * @param restTime - 每导入blockSize条数据后要休息restTime毫秒
	 */
	public void importFileDataSlowly(String filePath, int blockSize, int restTime) {
		File f = new File(filePath);
		try {
			BufferedReader br = new BufferedReader(new FileReader(f));
			int i = 0;
			int k = 0;
			while (br.ready()) {
				String str = br.readLine();
				TrackPoint2 tp = TrackPoint2.parseTP(str);
				updateService.update(tp);
				i++;
				k++;
				if (k % blockSize == 0) {
					k = 0;
					try {
						Thread.sleep(restTime);
						System.gc();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void importFileData(String filePath) {
		importFileData(filePath, Integer.MAX_VALUE);
	}
	
//	//将文件中的数据导入redis
//	public void importFileData(String filePath, int limit) {
////		try {
////			Thread.sleep(1000);
////		} catch (InterruptedException e1) {
////			e1.printStackTrace();
////		}
//		File f = new File(filePath);
//		try {
//			BufferedReader br = new BufferedReader(new FileReader(f));
//			int i = 0;
//			int k = 0;
//			while (i < limit && br.ready()) {
//				String str = br.readLine();
//				TrackPoint2 tp = TrackPoint2.parseTP(str);
//				updateService.update(tp);
//				i++;
//				k++;
//				if (k % groupLength == 0) {
//					k = 0;
//					try {
//						Thread.sleep(cycle);						
//					} catch (InterruptedException e) {
//						e.printStackTrace();
//					}
//				}
//			}
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		
//	}
	//将文件中的数据导入redis
	public void importFileData(String filePath, int limit) {
		File f = new File(filePath);
		try {
			BufferedReader br = new BufferedReader(new FileReader(f));
			int i = 0;
			int k = 0;
			while (i < limit && br.ready()) {
				String str = br.readLine();
				TrackPoint2 tp = TrackPoint2.parseTP(str);
				updateService.update(tp);
//				i++;
				k++;
//				if (k % groupLength == 0) {
//					k = 0;
//					i++;
//					System.out.println("import " + i * groupLength + " items");
//				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	public void setUpdateService(UpdateService updateService) {
		this.updateService = updateService;
	}

//	public void setTrajDataService(TrajDataService trajDataService) {
//		this.trajDataService = trajDataService;
//	}
	
	
} 
